package com.andy.wan.android.mvp.ui.fragment;

import android.view.View;
import android.widget.TextView;

import com.andy.ui.base.BaseListFragment;
import com.andy.ui.base.SimpleWebActivity;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.FeedArticleData;
import com.andy.wan.android.mvp.bean.NavigationListData;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.NavigationAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.donkingliang.labels.LabelsView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @Author: luofan
 * @CreateDate: 2020/4/1 16:19
 * @Description:
 */
public class NavigationFragment extends BaseListFragment<BaseResponse<List<NavigationListData>>, NavigationListData> {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.labels)
    LabelsView mLabels;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_navigation;
    }

    @NonNull
    @Override
    protected Observable<BaseResponse<List<NavigationListData>>> getObservable() {
        return RetrofitUtil.getInstance().create(WanAndroidApi.class).getNavigationListData();
    }

    @NonNull
    @Override
    protected List<NavigationListData> getRecyclerDataFromResult(BaseResponse<List<NavigationListData>> response) {
        return response.getData();
    }

    @Override
    public void initLogic() {
        super.initLogic();
        mLabels.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                FeedArticleData feedArticleData = (FeedArticleData) data;
                SimpleWebActivity.startMe(getContext(), feedArticleData.getLink(), feedArticleData.getTitle());
            }
        });
    }

    @NonNull
    @Override
    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Nullable
    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return null;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<NavigationListData, BaseViewHolder> getAdapter() {
        NavigationAdapter navigationAdapter = new NavigationAdapter();
        navigationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                navigationAdapter.setSelectPosition(position);
                initLabels(navigationAdapter.getItem(position));
            }
        });
        return navigationAdapter;
    }

    private void initLabels(NavigationListData data) {
        mLabels.setLabels(data.getArticles(), new LabelsView.LabelTextProvider<FeedArticleData>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, FeedArticleData data) {
                return data.getTitle();
            }
        });
    }

    @Override
    protected void initRecyclerData(List<NavigationListData> list) {
        super.initRecyclerData(list);
        if (list != null && list.size() > 0) {
            initLabels(list.get(0));
        }
    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }
}
